package com.aiti.lulian.controller;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.page.PageResult;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.gxmh.dto.GxmhCRMParamDto;
import com.aiti.lulian.gxmh.dto.GxmhEnterpriseSearchParamDto;
import com.aiti.lulian.gxmh.dto.XLBEnterpriseLinkParamDto;
import com.aiti.lulian.gxmh.dto.XLBSerachWrapper;
import com.aiti.lulian.gxmh.entity.GxmhTkCurrentSearch;
import com.aiti.lulian.gxmh.service.GxmhEnterpriseSearchService;
import com.aiti.lulian.gxmh.service.impl.GxmhXLBService;
import com.aiti.lulian.gxmh.vo.GxmhEnterpriseSearchResultVo;
import com.aiti.lulian.gxmh.vo.GxmhUserVisitHistoryVo;
import com.aiti.lulian.gxmh.vo.GxmhYunyangDataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * description:
 * auth fcb
 * date 2025-06-10 14:53
 **/
@RestController
@RequestMapping("gxmhEnterpriseSearch")
@Slf4j
@Api(value = "工信门户--企业搜索", tags = "工信门户--企业搜索")
public class GxmhEnterpriseSearchController {

    @Autowired
    private GxmhEnterpriseSearchService  gxmhEnterpriseSearchService;
    @Autowired
    private GxmhXLBService gxmhXLBService;

    @PostMapping("/yzqy")
    @ApiOperation(value = "查询优质企业")
    public Message queryEnterpriseInfo(@RequestBody GxmhEnterpriseSearchParamDto queryDto,
                                       @RequestParam(value = "userId", required = false)String userId,
                                       @RequestParam(value = "countyAreaCode", required = false)String countyAreaCode) {
        try {
            PageResult<GxmhEnterpriseSearchResultVo> gxmhEnterpriseSearchResultVoPageResult =
                    gxmhEnterpriseSearchService.gxmhEnterpriseSearch(queryDto, userId, countyAreaCode);
            return Message.success(gxmhEnterpriseSearchResultVoPageResult);
        }catch (Exception e) {
            log.error("查询异常：{}", ExceptionUtil.stacktraceToString(e));
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("查询失败");
        }
    }

    @GetMapping("/indexEnterpriseInfo")
    @ApiOperation(value = "查询首页企业信息")
    public Message queryIndexEnterpriseInfo(@RequestParam("enterpriseType")Integer enterpriseType) {
        try {
            List<GxmhEnterpriseSearchResultVo> gxmhEnterpriseSearchResultVoList =
                    gxmhEnterpriseSearchService.queryIndexEnterpriseInfo(enterpriseType);
            return Message.success(gxmhEnterpriseSearchResultVoList);
        }catch (Exception e) {
            log.error("查询异常：{}", ExceptionUtil.stacktraceToString(e));
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("查询失败");
        }
    }

    @GetMapping("/xlb/search/meta")
    @ApiOperation(value = "小蓝本高级搜索元数据")
    public Message requestSearchMeta(@RequestParam(value = "userId", required = false)String userId) {
        try {
            return gxmhXLBService.requestSearchMeta(userId);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("接口异常");
        }
    }


    @PostMapping("/xlb/search")
    @ApiOperation(value = "小蓝本高级搜索")
    public Message requestSearch(@RequestBody XLBSerachWrapper wrapper) {
        try {
            return gxmhXLBService.requestSearch(wrapper);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("接口异常");
        }
    }

    @GetMapping("/xlb/enterpriseInfoByEid")
    @ApiOperation(value = "小蓝本查询企业信息-eid")
    public Message requestEnterpriseInfoByEid(@RequestParam(value = "eid") String eid, @RequestParam(value = "userId")String userId) {
        return gxmhXLBService.requestEnterpriseInfoByEid(eid, userId);
    }

    @GetMapping("/xlb/enterpriseShareholderInfoByEid")
    @ApiOperation(value = "小蓝本查询企业股东信息-eid")
    public Message requestEnterpriseShareholderInfoByEid(@RequestParam(value = "eid") String eid,
                                                         @RequestParam(value = "pageIndex") Integer pageIndex,
                                                         @RequestParam(value = "pageSize") Integer pageSize) {
        return gxmhXLBService.requestEnterpriseShareholderInfoByEid(eid, pageIndex, pageSize);
    }

    @GetMapping("/xlb/enterpriseInvestmentInfoByEid")
    @ApiOperation(value = "小蓝本查询企业投资信息-eid")
    public Message requestEnterpriseInvestmentInfoByEid(@RequestParam(value = "eid") String eid,
                                                        @RequestParam(value = "pageIndex") Integer pageIndex,
                                                        @RequestParam(value = "pageSize") Integer pageSize) {
        return gxmhXLBService.requestEnterpriseInvestmentInfoByEid(eid, pageIndex, pageSize);
    }

    @GetMapping("/xlb/enterpriseGroupMemberInfoByEid")
    @ApiOperation(value = "小蓝本查询集团成员信息-eid")
    public Message requestEnterpriseGroupMemberInfoByEid(@RequestParam(value = "eid") String eid,
                                                         @RequestParam(value = "pageIndex") Integer pageIndex,
                                                         @RequestParam(value = "pageSize") Integer pageSize) {
        return gxmhXLBService.requestEnterpriseGroupMemberInfoByEid(eid, pageIndex, pageSize);
    }

    @PostMapping("/xlb/enterpriseLinkInfoByEid")
    @ApiOperation(value = "小蓝本查询企业联系方式-eid")
    public Message requestEnterpriseLinkInfoByEid(@RequestBody XLBEnterpriseLinkParamDto dto) {
        return gxmhXLBService.requestEnterpriseLinkInfoByEid(dto);
    }

    @PostMapping("/xlb/enterpriseLinkInfoByEidFromTopKp")
    @ApiOperation(value = "小蓝本查询企业联系方式-eid")
    public Message requestEnterpriseLinkInfoByEidFromTopKp(@RequestBody XLBEnterpriseLinkParamDto dto) {
        return gxmhXLBService.requestEnterpriseLinkInfoByEidTopKP(dto);
    }

    @GetMapping("/xbl/enterpriseNetShopByEid")
    @ApiOperation(value = "小蓝本查询企业网店信息-eid")
    public Message requestEnterpriseNetShopByEid(@RequestParam(value = "eid")String eid,
                                                 @RequestParam(value = "pageIndex") Integer pageIndex,
                                                 @RequestParam(value = "pageSize") Integer pageSize,
                                                 @RequestParam(value = "type", required = false)String type) {
        return gxmhXLBService.requestEnterpriseNetShopByEid(eid, pageIndex, pageSize, type);
    }

    @GetMapping("/xbl/enterpriseAppByEid")
    @ApiOperation(value = "小蓝本查询企业APP应用信息-eid")
    public Message requestEnterpriseAppByEid(@RequestParam(value = "eid")String eid,
                                             @RequestParam(value = "pageIndex") Integer pageIndex,
                                             @RequestParam(value = "pageSize") Integer pageSize) {
        return gxmhXLBService.requestEnterpriseAppByEid(eid, pageIndex, pageSize);
    }

    @GetMapping("/xbl/enterpriseNewMediaByEid")
    @ApiOperation(value = "小蓝本查询企业新媒体信息-eid")
    public Message requestEnterpriseNewMediaByEid(@RequestParam(value = "eid")String eid,
                                             @RequestParam(value = "pageIndex") Integer pageIndex,
                                             @RequestParam(value = "pageSize") Integer pageSize) {
        return gxmhXLBService.requestEnterpriseNewMediaByEid(eid, pageIndex, pageSize);
    }

    @GetMapping("/xlb/requestEnterpriseAlterRecordByEid")
    @ApiOperation(value = "小蓝本查询企业变更记录信息-eid")
    public Message requestEnterpriseAlterRecordByEid(@RequestParam(value = "eid")String eid,
                                             @RequestParam(value = "pageIndex") Integer pageIndex,
                                             @RequestParam(value = "pageSize") Integer pageSize,
                                             @RequestParam(value = "beginAltdate", required = false)String beginAltdate,
                                             @RequestParam(value = "altItemType", required = false)String altItemType) {

        return gxmhXLBService.requestEnterpriseAlterRecordByEid(eid, pageIndex, pageSize, beginAltdate, altItemType);
    }

    @GetMapping("/xlb/requestEnterpriseAggZhiChanInfoByEid")
    @ApiOperation(value = "查询企业知识产权统计")
    public Message requestEnterpriseAggZhiChanInfoByEid(@RequestParam(value = "eid") String eid) {
        return gxmhXLBService.requestEnterpriseAggZhiChanInfoByEid(eid);
    }

    @GetMapping("/xlb/requestEnterprisePagerParentInfoByEid")
    @ApiOperation(value = "查询知识产权--专利")
    public Message requestEnterprisePagerParentInfo(@RequestParam(value = "eid")String eid,
                                                    @RequestParam(value = "pageIndex") Integer pageIndex,
                                                    @RequestParam(value = "pageSize") Integer pageSize) {
        return gxmhXLBService.requestEnterprisePagerParentInfo(eid, pageIndex, pageSize);
    }

    @GetMapping("/xlb/requestEnterprisePagerTrademarksByEid")
    @ApiOperation(value = "查询知识产权--商标")
    public Message requestEnterprisePagerTrademarksByEid(@RequestParam(value = "eid")String eid,
                                                         @RequestParam(value = "pageIndex") Integer pageIndex,
                                                         @RequestParam(value = "pageSize") Integer pageSize) {
        return gxmhXLBService.requestEnterprisePagerTrademarksByEid(eid, pageIndex, pageSize);
    }

    @GetMapping("/xlb/requestEnterprisePagerSoftwareCopyrightByEid")
    @ApiOperation(value = "查询知识产权--软件著作权")
    public Message requestEnterprisePagerSoftwareCopyrightByEid(@RequestParam(value = "eid")String eid,
                                                                @RequestParam(value = "pageIndex") Integer pageIndex,
                                                                @RequestParam(value = "pageSize") Integer pageSize,
                                                                @RequestParam(value = "year", required = false) String year) {

        return gxmhXLBService.requestEnterprisePagerSoftwareCopyrightByEid(eid, pageIndex, pageSize, year);
    }

    @GetMapping("/xlb/requestEnterprisePagerWorkCopyrightByEid")
    @ApiOperation(value = "查询知识产权--作品著作权")
    public Message requestEnterprisePagerWorkCopyrightByEid(@RequestParam(value = "eid")String eid,
                                                           @RequestParam(value = "pageIndex") Integer pageIndex,
                                                           @RequestParam(value = "pageSize") Integer pageSize,
                                                           @RequestParam(value = "year", required = false) String year) {

        return gxmhXLBService.requestEnterprisePagerWorkCopyrightByEid(eid, pageIndex, pageSize, year);
    }

    @GetMapping("/xlb/requestEnterprisePagerWebSitesByEid")
    @ApiOperation("查询知识产权--查询网站信息")
    public Message requestEnterprisePagerWebSitesByEid(@RequestParam(value = "eid") String eid,
                                                      @RequestParam(value = "pageIndex") Integer pageIndex,
                                                      @RequestParam(value = "pageSize") Integer pageSize) {

        return gxmhXLBService.requestEnterprisePagerWebSitesByEid(eid, pageIndex, pageSize);
    }

    @RequestMapping(value = "/xlb/requestEnterpriseZhaopinAndRiskByEid")
    @ApiOperation(value = "查询企业风险信息数据")
    public Message requestEnterpriseZhaopinAndRiskByEid(@RequestParam(value = "eid") String eid) {
        return gxmhXLBService.requestEnterpriseZhaopinAndRiskByEid(eid);
    }

    @RequestMapping(value = "/xlb/requestEnterprisePagerCasePubByEid")
    @ApiOperation(value = "查询企业的行政处罚信息")
    public Message requestEnterprisePagerCasePubByEid(@RequestParam(value = "eid") String eid,
                                                     @RequestParam(value = "pageIndex") Integer pageIndex,
                                                     @RequestParam(value = "pageSize") Integer pageSize,
                                                     @RequestParam(value = "illegActType", required = false)String illegActType) {
        return gxmhXLBService.requestEnterprisePagerCasePubByEid(eid, pageIndex, pageSize, illegActType);
    }

    @RequestMapping(value = "/xbl/requestEnterprisePagerCourtAnnouncementsByEid")
    @ApiOperation(value = "查询企业法院公告")
    public Message requestEnterprisePagerCourtAnnouncementsByEid(@RequestParam(value = "eid") String eid,
                                                                 @RequestParam(value = "pageIndex") Integer pageIndex,
                                                                 @RequestParam(value = "pageSize") Integer pageSize,
                                                                 @RequestParam(value = "annType", required = false)String annType) {
        return gxmhXLBService.requestEnterprisePagerCourtAnnouncementsByEid(eid, pageIndex, pageSize, annType);
    }

    @GetMapping("/xbl/requestEnterprisePagerCourtPjwsByEid")
    @ApiOperation("请求企业裁判文书")
    public Message requestEnterprisePagerCourtPjwsByEid(@RequestParam(value = "eid")String eid,
                                                        @RequestParam(value = "pageIndex")Integer pageIndex,
                                                        @RequestParam(value = "pageSize")Integer pageSize,
                                                        @RequestParam(value = "type", required = false)String type) {

        return gxmhXLBService.requestEnterprisePagerCourtPjwsByEid(eid, pageIndex, pageSize, type);
    }

    @GetMapping("/xlb/requestEnterprisePagerCourtSessionAnnouncementsByEid")
    @ApiOperation(value = "查询企业开庭公告")
    public Message requestEnterprisePagerCourtSessionAnnouncementsByEid(@RequestParam(value = "eid")String eid,
                                                                        @RequestParam(value = "pageIndex")Integer pageIndex,
                                                                        @RequestParam(value = "pageSize")Integer pageSize) {
        return gxmhXLBService.requestEnterprisePagerCourtSessionAnnouncementsByEid(eid, pageIndex, pageSize);
    }

    @GetMapping(value = "/xlb/requestEnterpriseZhaoTouBiaoDataByEid")
    @ApiOperation(value = "查询该企业招投标数据--分类数量")
    public Message requestEnterpriseZhaoTouBiaoDataByEid(@RequestParam(value = "eid")String eid) {
        try {
            List<GxmhYunyangDataVo> list = gxmhXLBService.requestEnterpriseYunyangDataByEid(eid);
            return Message.success(list);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询招投标数据异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常："+ e.getMessage());
        }
    }

    @ApiOperation(value = "查询招投标数据--列表数据")
    @GetMapping("/xlb/requestEnterpriseZhaoTouBiaoByEid")
    public Message requestEnterpriseZhaoTouBiaoByEid(@RequestParam("eid") String eid) {
        try {
            return Message.success(gxmhXLBService.requestEnterpriseZhaoTouBiaoByEid(eid));
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询招投标数据异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常："+ e.getMessage());
        }
    }

    @GetMapping("/xbl/requestEnterpriseEmail")
    @ApiOperation(value = "查询企业邮箱")
    public Message requestEnterpriseEmail(@RequestParam(value = "eid") String eid) {
        return gxmhXLBService.requestEnterpriseEmail(eid);
    }

 //-----------------------------------------------------------------------------------------------
    @GetMapping("/accessEnterpriseLink")
    @ApiOperation(value = "是否可以查看企业的联系人")
    public Message accessEnterpriseLink(@RequestParam(value = "userid") String userid,
                                        @RequestParam(value = "eid")String eid) {
        try {
            gxmhXLBService.accessEnterpriseLink(userid, eid);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("是否可以查看企业的联系人异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常");
        }
    }

    @GetMapping("/EnterpriseLinkLock")
    @ApiOperation(value = "企业是否已经解锁联系人")
    public Message EnterpriseLinkLock(@RequestParam(value = "userId") String userid,
                                      @RequestParam(value = "eid")String eid) {
        try {
            return Message.success(gxmhXLBService.enterpriseUnlockLink(userid, eid));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("企业是否已经解锁联系人：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常");
        }
    }

    @PostMapping("/addEnterpriseToCRM")
    @ApiOperation(value = "添加企业到CRM")
    public Message addEnterpriseToCRM(@RequestBody GxmhCRMParamDto dto) {
        try {
            gxmhXLBService.addEnterpriseToCRM(dto);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("添加企业到CRM异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常");
        }
    }

    //--------------------------------------用户最近搜索--------------------------------------------------
    @GetMapping("/queryUserCurrentSearch")
    @ApiOperation(value = "查询用户最近搜索")
    public Message queryUserCurrentSearch(@RequestParam(value = "userId") String userId) {
        try {
            List<GxmhTkCurrentSearch> gxmhTkCurrentSearchList = gxmhXLBService.queryCurrentSearchList(userId);
            return Message.success(gxmhTkCurrentSearchList);
        }catch (Exception e) {
            log.error("查询用户最近搜索异常：{}", ExceptionUtil.stacktraceToString(e));
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("接口异常");
        }
    }
    @PostMapping("/deleteUserCurrentSearch")
    @ApiOperation(value = "删除用户最近搜索")
    public Message deleteUserCurrentSearch(@RequestBody List<String> baseIdList) {
        try {
            gxmhXLBService.deleteUserCurrentSearch(baseIdList);
            return Message.success();
        }catch (Exception e) {
            log.error("删除用户最近搜索异常：{}", ExceptionUtil.stacktraceToString(e));
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("接口异常");
        }
    }

    //-------------------------------------------热门搜索史----------------------------------------------
    @GetMapping("/hotSearchKeywordList")
    @ApiOperation(value = "热门搜索")
    public Message hotSearchKeywordList() {
        try {
            List<String> keywordList = gxmhXLBService.hotSearchKeywordList();
            return Message.success(keywordList);
        }catch (Exception e) {
            log.error("热门搜索异常：{}", ExceptionUtil.stacktraceToString(e));
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("接口异常");
        }
    }

    //-------------------------------------------用户浏览记录----------------------------------------------
    @GetMapping("/userVisitHistory")
    @ApiOperation(value = "查询用户浏览记录")
    public Message addUserVisitHistory(@RequestParam(value = "userId") String userId) {
        try {
            List<GxmhUserVisitHistoryVo> gxmhUserVisitHistoryVos = gxmhXLBService.queryUserVisitHistoryList(userId);
            return Message.success(gxmhUserVisitHistoryVos);
        }catch (Exception e) {
            log.error("查询用户浏览记录：{}", ExceptionUtil.stacktraceToString(e));
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("接口异常");
        }
    }

    @PostMapping("/deleteUserCurrentVisitHistory")
    @ApiOperation(value = "删除用户最近搜索")
    public Message deleteUserCurrentVisitHistory(@RequestBody List<String> baseIdList) {
        try {
            gxmhXLBService.deleteUserCurrentVisitHistory(baseIdList);
            return Message.success();
        }catch (Exception e) {
            log.error("删除用户最近浏览历史异常：{}", ExceptionUtil.stacktraceToString(e));
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            return Message.fail("接口异常");
        }
    }

    @GetMapping("/getEnterpriseLinkCount")
    @ApiOperation(value = "获取企业解锁联系人数量")
    public Message getEnterpriseLinkCount(@RequestParam(value = "enterpriseId") String enterpriseId) {
        try {
            Integer count = gxmhXLBService.getEnterpriseLinkCount(enterpriseId);
            return Message.success(count);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取企业解锁联系人数量异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常");
        }
    }

    @GetMapping("/getEnterpriseMiniProgramVersion")
    @ApiOperation(value = "获取小程序版本")
    public Message getEnterpriseMiniProgramVersion(@RequestParam(value = "enterpriseId") String enterpriseId) {
        try {
            Integer version = gxmhXLBService.getEnterpriseMiniProgramVersion(enterpriseId);
            return Message.success(version);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取小程序版本异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常");
        }
    }


}
